package com.lanp.springsd.controller;

import com.github.pagehelper.PageHelper;
import com.lanp.springsd.dao.ProductMapper;
import com.lanp.springsd.model.Product;
import com.lanp.springsd.model.ProductExample;
import com.lanp.springsd.model.TOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductRest {
    @Autowired
    private ProductMapper productMapper;

    /**
     * 入库，根据规则分片入库
     *
     * @param product
     * @return
     */
    @PostMapping("/add/a")
    public Object adda(Product product) {
        int i = productMapper.insert(product);
        // TODO:
        return i;
    }

    /**
     * 获取商品id详情
     *
     * @param id
     * @return
     */
    @GetMapping("/get/detail")
    public Object getDetail(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取商品列表
     *
     * @param product
     * @return
     */
    @PostMapping("/get/list")
    public Object getDetail(Product product) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        if (product.getId() != null) {
            criteria.andIdEqualTo(product.getId());
        }
        if (product.getName() != null) {
            criteria.andNameLike(product.getName());
        }
        if (product.getUserId() != null) {
            criteria.andUserIdEqualTo(product.getUserId());
        }
        if (product.getCityId() != null) {
            criteria.andCityIdEqualTo(product.getCityId());
        }
        PageHelper.startPage(product.getPageNumber(), product.getPageSize());
        return productMapper.selectByExample(productExample);
    }

}
